package cate.game.role.show.msg;

import cate.common.table.d.GD;
import cate.common.table.show.row.LeaderSkinRow;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBBlog;
import cate.game.role.Role;
import cate.game.role.show.leaderskin.LeaderSkin;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@ClientMsg(title = "主角皮肤 の 请求")
public class LeaderSkinOnReq implements MsgInput {

	@NoteField("皮肤id")
	public int tid;

	@NoteField("是否改变了性别")
	public boolean changeSex = false;

	@Override
	public void execute(GameBody game, ClientContext cc) {
		EcResult r =  settle(cc.getRole());
		game.notice.message(cc.getRole(), r, true);
	}

	private EcResult settle(Role role) {
		EcResult r = new EcResult();
		if (tid > 0) {
			LeaderSkin leaderSkin = role.getShow().leaderSkin.getItem(tid);
			if (leaderSkin == null) {
				return r.fail("皮肤未激活");
			}
			LeaderSkinRow row = leaderSkin.getRow(role.getGame());
			if (row == null) {
				return r.fail("配置不存在");
			}
		}
		role.getBase().leaderSkin = tid;
		role.sendNow(new LeaderSkinOnResp(tid));
		if(changeSex){
			role.getGame().notice.message(role, "穿戴成功，性别更改为" + GD.Gender.description(role.getBase().sex));
		}else {
			role.getGame().notice.message(role, "穿戴成功");
		}
		return r.success();
	}

	@Override
	public MsgConfig conf() {
		return MsgIdBBlog.LEADER_SKIN_ON_REQ;
	}
}
